package com.qfedu.controller;

import com.qfedu.constants.Status;
import com.qfedu.model.Branch;
import com.qfedu.model.TaskPlan;
import com.qfedu.page.PageResult;
import com.qfedu.page.QueryPageBean;
import com.qfedu.page.Result;
import com.qfedu.service.IBranchService;
import com.qfedu.service.ITaskPlanService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 概述:
 *
 * @Author: Tbh
 * @Date: 2022/6/30 20:33
 */

@RestController
@RequestMapping("taskPlan")
public class TaskPlanController {

    @Autowired
    private ITaskPlanService taskPlanService;

    /**
     * @Author Tbh.
     * @Description: 任务计划分页查询
     * @Date 2022/7/1 15:46
     * @Param QueryPageBean
     * @return PageResult
     */
    @PostMapping("all")
    public PageResult all(@RequestBody QueryPageBean queryPage) {
        PageResult page = taskPlanService.findPage(queryPage);
        return page;
    }

    /**
     * @Author Tbh.
     * @Description: 增加任务计划
     * @Date 2022/7/1 15:47
     * @Param Branch
     * @return Result
     */
    @PostMapping("add")
    public Result addTaskPlan(@RequestBody TaskPlan taskPlan) {
        int row = taskPlanService.addTaskPlan(taskPlan);
        if (row > 0) {
            //枚举类名.对象名.get方法 获取到对应的属性值
            //Status.SUCCESS.name();  获取到枚举对象名  SUCCESS
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    /**
     * @Author Tbh.
     * @Description: 数据回显
     * @Date 2022/7/1 15:45
     * @Param tid
     * @return Result
     */
    @GetMapping("findByTid")
    public Result findByTid(@RequestParam("tid") String tid) {
        TaskPlan taskPlan  = taskPlanService.findByTid(tid);
        Result result = new Result();
        result.setMessage(Status.FAIL.getMessage());
        if (ObjectUtils.isNotEmpty(taskPlan)) {
            result.setFlag(true);
            result.setData(taskPlan);
            result.setMessage(Status.SUCCESS.getMessage());
        }
        return result;
    }

    /**
     * @Author Tbh.
     * @Description: 修改任务计划
     * @Date 2022/7/1 16:48
     * @Param Branch
     * @return Result
     */
    @PostMapping("edit")
    public Result edit(@RequestBody TaskPlan taskPlan) {
        int row = taskPlanService.edit(taskPlan);
        if (row > 0) {
            return Result.success();
        }
        return Result.fail();
    }

    /**
     * @Author Tbh.
     * @Description: 逻辑删除
     * @Date 2022/7/1 16:48
     * @Param
     * @return Result
     */
    @GetMapping("delete")
    public Result delete(@RequestParam("tid") String tid) {
        int row = taskPlanService.deleteByTid(tid);
        if (row > 0) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }
}
